package com.supermap.fabricsdk;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.FileUtils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.security.Principal;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.List;

/**
 * @author liqs
 * @version 1.0
 * @date 2020/6/15 13:38
 *
 * 主要为解析日志审查服务
 */
public class SmLoggerService {

    // 区块链证书进行解码得到证书的组织信息
    public JSONObject getUserInfo(File pemFile){
        JSONObject json = new JSONObject();
        String s = testOu(pemFile);
        try {
            InputStream inputStream = new ByteArrayInputStream(s.getBytes());
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            X509Certificate certificate = (X509Certificate) cf.generateCertificate(inputStream);
            Principal principal = certificate.getSubjectDN();
            String principalName = principal.getName();// 这里面获取具体的用户名
            Principal issuerDN = certificate.getIssuerDN();   // 这里面获取组织
            String issuerDNName = issuerDN.getName();
            String user = getUser(principalName);
            String org = getOrg(issuerDNName);
            json.put("Org", org);
            json.put("User", user);
        } catch (CertificateException E){

        }
        return json;
    }

    // 区块链证书进行解码得到证书的组织信息
    public X509Certificate getcertificate(File pemFile){
        X509Certificate certificate = null;
        String s = testOu(pemFile);
        try {
            InputStream inputStream = new ByteArrayInputStream(s.getBytes());
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            certificate = (X509Certificate) cf.generateCertificate(inputStream);
        } catch (CertificateException E){

        }
        return certificate;
    }

    // 读取文件得到证书信息的 String 信息
    private String testOu(File pemFile){
        StringBuilder stringBuilder = new StringBuilder();
        // 写到临时文件
        try {
            String initString = FileUtils.readFileToString(pemFile);
            String[] strings = initString.split("\n");
            FileWriter fw = new FileWriter("out.pem");
            int line = 1;
            for (String s : strings) {
                if (line == 1) {
                    fw.write("-----BEGIN CERTIFICATE-----" + "\n");             // 一行行获取数据的一种方式
                    stringBuilder.append("-----BEGIN CERTIFICATE-----" + "\n");     // 因为是换行，所以采用StringBuilder追加形成
                } else {
                    fw.write(s + "\n");
                    stringBuilder.append(s + "\n");
                }
                line++;
            }
            fw.close();
        }catch (Exception E){
        }
        return stringBuilder.toString();
    }

    // 获取用户信息
    private String getUser(String str){
        List<String> result = Arrays.asList(str.split(","));
        String cn = result.get(0);
        cn = cn.trim();
        while (cn.startsWith("　")) {
            cn = cn.substring(1, cn.length()).trim();
        }
        return cn.substring(3, cn.length());
    }

    // 获取组织信息
    private String getOrg(String str){
        List<String> result = Arrays.asList(str.split(","));
        String cn = result.get(1);
        cn = cn.trim();
        while (cn.startsWith("　")) {
            cn = cn.substring(1, cn.length()).trim();
        }
        return cn.substring(2, cn.length());
    }

    // 对 链码执行的 docker 日志进行解析

}
